package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.ProductCatalog;
import cn.wolfcode.trip.base.query.ProductCatalogQuery;
import cn.wolfcode.trip.base.service.IProductCatalogService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商城目录资源后台控制器
 */
@Controller
@RequestMapping("productCatalog")
public class ProductCatalogController {

    @Autowired
    private IProductCatalogService productCatalogService;

    /**
     * 商城目录页面
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") ProductCatalogQuery qo, Model model){
        //按序号升序
        qo.setOrderBy("sequence");
        model.addAttribute("pageInfo",productCatalogService.query(qo));
        return "productCatalog/list";
    }

    /**
     * 新增/修改
     * @param productCatalog
     * @param file
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(ProductCatalog productCatalog, MultipartFile file){
        if(file!=null&&file.getSize()>0){
            String url = UploadUtil.uploadQiniu(file);
            productCatalog.setCoverUrl(url);
        }
        productCatalogService.saveOrUpdate(productCatalog);
        return new JsonResult();
    }
}
